package com.zllkj.mm.widgets;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 倒计时按键
 */

public class CountdownButton extends Button implements View.OnClickListener {

    // 倒计时时长，默认倒计时时间60s
    private long length = 60 * 1000;
    // 开始执行计时的类，可以再每秒执行间隔任务
    private Timer timer;

    private TimerTask timerTask;

    private String beforeText = "获取验证码";

    private String afterText = "秒重新获取";

    private boolean isRunning = false;

    private OnClickListener onClickListener;

    public CountdownButton(Context context) {
        super(context);
        initView();
    }

    public CountdownButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public CountdownButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }


    private void initView() {
        if (!TextUtils.isEmpty(getText())) {
            beforeText = getText().toString().trim();
        }
        this.setText(beforeText);
        setOnClickListener(this);
    }



    /**
     * 设置倒计时时长
     * @param length
     */
    public void setLength(long length) {
        this.length = length;
    }

    /**
     * 设置未点击时的文字
     * @param beforeText
     */
    public void setBeforeText(String beforeText) {
        this.beforeText = beforeText;
    }

    /**
     * 设置点击后显示的文字
     * @param afterText
     */
    public void setAfterText(String afterText) {
        this.afterText = afterText;
    }


    @Override
    public void setOnClickListener(OnClickListener onClickListener) {
        if (onClickListener instanceof CountdownButton) {
            super.setOnClickListener(onClickListener);
        } else {
            this.onClickListener = onClickListener;
        }
    }

    /**
     * 点击按钮后的操作
     * @param v
     */
    @Override
    public void onClick(View v) {
        start();
        isRunning = true;
        if (onClickListener != null) {
            onClickListener.onClick(v);
        }
    }




    /**
     * 开始倒计时
     */
    public void start() {
        initTimer();
        this.setText("" + length / 1000 + afterText);
        this.setEnabled(false);
        timer.schedule(timerTask, 0, 1000);
    }

    /**
     * 初始化时间
     */
    private void initTimer() {
        timer = new Timer();
        timerTask = new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(1);
            }
        };
    }



    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            CountdownButton.this.setText("" + length / 1000 + afterText);
            length -= 1000;
            if (length < 0) {
                CountdownButton.this.setEnabled(true);
                CountdownButton.this.setText(beforeText);
                clearTimer();
                length = 60 * 1000;
            }

            return false;
        }
    });


    private void clearTimer() {
        isRunning = false;
        if (timerTask != null) {
            timerTask.cancel();
            timerTask = null;
        }

        if (timer != null) {
            timer.cancel();
            timer = null;
        }

    }

    @Override
    protected void onDetachedFromWindow() {
        clearTimer();
        super.onDetachedFromWindow();
    }


    public boolean isRunning() {
        return isRunning;
    }


}
